package com.uubx.service.impl;

import com.uubx.mapper.UserMapper;
import com.uubx.pojo.User;
import com.uubx.service.UserService;
import com.uubx.utils.Md5Utils;
import com.uubx.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author wuguojun
 * @Date 2020/12/26 18:53
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 分页查询
     * @param
     * @param
     * @return
     */
    @Override
    public List<User> queryAllUser(UserVo userVo) {
        List<User> users = userMapper.queryAllUser(userVo);
        return users;
    }

    @Override
    public Long queryAllUserCount(UserVo userVo) {

        Long count = userMapper.queryAllUserCount(userVo);
        return count;
    }



    @Override
    public List<User> queryUserByPhone(String phone) {
        List<User> users = userMapper.queryUserByPhone(phone);
        return users;
    }

    @Override
    public List<User> queryUserByStatus(UserVo userVo) {
        List<User> users = userMapper.queryUserByStatus(userVo);
        return users;
    }

    @Override
    public Long queryUserByStatusCount(Integer status) {
        Long count = userMapper.queryUserByStatusCount(status);
        return count;
    }


    @Override
    public void updateUserByStatus(Integer id,Integer status) {
        if (status==1){
            status=0;
            userMapper.updateUserByStatus(id,status);
        }else {
            status=1;
            userMapper.updateUserByStatus(id,status);
        }
    }

    @Override
    public User queryUserById(Integer id) {
        User user = userMapper.queryUserById(id);
        return user;
    }

    @Override
    public void addUser(User user) {
        user.setCreateTime(new Date());
        user.setLastLoginTime(new Date());
        String salt = Md5Utils.createSalt();
        user.setSalt(salt);
        String password = user.getPassword();
        String md5Password = Md5Utils.md5Password(password, salt);
        user.setPassword(md5Password);
        userMapper.addUser(user);
    }

    @Override
    public void updateUser(User user) {
        if (user.getPassword()!=null){
            String salt = Md5Utils.createSalt();
            user.setSalt(salt);
            user.setPassword(Md5Utils.md5Password(user.getPassword(),user.getSalt()));
        }
        userMapper.updateUser(user);
    }

}
